package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    //注入远程调用接口
    @Autowired
    private CustomerInfoFeignClient client;

    //redis
    @Autowired
    private RedisTemplate redisTemplate;

    /*
    * 微信小程序登录
    * */
    @Override
    public String login(String code) {
        //用code进行远程调用，返回用户id
        Result<Long> loginResult = client.login(code);
        //判断，如果返回失败，则返回错误的提示
        if (loginResult.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //获取远程调用返回的用户id
        Long customerId = loginResult.getData();
        //判断返回的id是否为空，为空报错
        if (customerId == null) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //生成token字符串
        String token = UUID.randomUUID().toString().replaceAll("-","");
        //把用户id放到redis存储，设置过期时间
        //key：user:login:+token  value： id
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,customerId.toString(),
                RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.HOURS);//设置过期时间
        //返回token字符串
        return token;
    }

    /*
    * 获取用户登录信息
    * */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {

        //查询redis中token对应的id
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        //判断是否过期
        if(!StringUtils.hasText(customerId)){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //远程调用
        Result<CustomerLoginVo> customerInfo = client.getCustomerInfo(Long.parseLong(customerId));

        if (customerInfo.getCode() != 200) {
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        CustomerLoginVo data = customerInfo.getData();
        if(data == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }

        return data;
    }

    /*
    * 更新用户手机号码
    * */
    @Override
    public Boolean updateWxPhone(UpdateWxPhoneForm updateWxPhoneForm) {
        Result<Boolean> booleanResult = client.updateWxPhoneNumber(updateWxPhoneForm);
        return booleanResult.getData();
    }
}
